package com.learn.mapstruct.mapping;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.learn.mapstruct.mapping.control.MappingMethodControl;
import com.learn.mapstruct.mapping.model.MappingEntity1;
import com.learn.mapstruct.mapping.model.MappingEntity2;
import com.learn.mapstruct.mapping.model.MappingEntity3;
import com.learn.mapstruct.mapping.model.MappingEntity4;
import org.mapstruct.*;

@Mapper(imports = {StrUtil.class})
public interface MappingMapper {

    @Mapping(target = "name2", constant = "fixedValue")
    @Mapping(target = "numValue", source = "num", numberFormat = "#.00")
    @Mapping(target = "name3"
        , expression = "java(entity1.getName().toUpperCase())"
    )
    @Mapping(target = "name4"
        , source = "name"
        , defaultExpression = "java(cn.hutool.core.util.RandomUtil.randomString(2))"
        , conditionExpression = "java(StrUtil.isNotBlank(entity1.getName()))"
    )
    @Mapping(target = "name5", source = "num", resultType = Long.class)
    @Mapping(target = "name6", source = "name", defaultValue = "defaultValue")
    @Mapping(target = "name7", source = "name", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
    @Mapping(target = "name8", source = "name", nullValueCheckStrategy = NullValueCheckStrategy.ON_IMPLICIT_CONVERSION)
    MappingEntity2 to(MappingEntity1 entity1);

    @Mapping(target = "name6", source = "name", dependsOn = {"id", "name"})
    MappingEntity2 to2(MappingEntity1 entity1);

    @Mapping(target = "id", source = "id", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    void to3(MappingEntity3 entity, @MappingTarget MappingEntity3 target);

    @Mapping(target = "id", source = "id", mappingControl = MappingMethodControl.class)
    MappingEntity4 clone(MappingEntity3 entity);


    default String toStr(Long value) {
        return value.toString();
    }

    default Long toLong(Double value) {
        return value.longValue();
    }

    default Long toLong2(Object value) {
        return Convert.toLong(value);
    }
}
